package com.zjw.forum.controller;

import com.zjw.forum.common.AppResult;
import com.zjw.forum.exception.ApplicationException;
import com.zjw.forum.model.User;
import com.zjw.forum.utils.JWTUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@Api("测试接口")
@RestController
@RequestMapping("/test")
public class TestController {
    @ApiOperation("打印Hello spring")
    @GetMapping("/t1")
    public String hello(){
        return "Hello spring";
    }
    @ApiOperation("抛出异常")
    @GetMapping("/t2")
    public AppResult getExc() throws Exception {
        throw new Exception("出现异常！");
    }
    @ApiOperation("抛出自定义异常")
    @GetMapping("/t3")
    public AppResult getAppE() throws Exception {
        throw new ApplicationException(AppResult.failed("appResult的异常信息"));
    }
    @ApiOperation("根据名字打招呼")
    @PostMapping("/t4")
    public String test(@ApiParam("姓名") @RequestParam("name") String name)  {
        return "hello! "+name;
    }

    @GetMapping("/t5")
    public User test() {
        User user = new User();
        user.setId(1L);
        user.setUsername(null); // 故意设置 null
        return user;
    }
    @GetMapping("/t6")
    public ResponseEntity<AppResult<User>> test6(){
        User user=new User();
        user.setId(1l);
        user.setNickname("王子健");
        String user_token1 = JWTUtil.genToken("user_token", user);
        //设置响应头
        // 创建响应头并添加 JWT
        HttpHeaders headers = new HttpHeaders();
        headers.add("user_token", user_token1);
        return new ResponseEntity<>(AppResult.success(user), headers, HttpStatus.OK);
    }
}
